//
//  YYCacheHelper.m
//  singleTarget
//
//  Created by   李小云 on 17/9/21.
//  Copyright © 2017年   李小云. All rights reserved.
//

#import "YYCacheHelper.h"
#import "YYCacheManger.h"
@implementation YYCacheHelper

+(instancetype)shareHelper
{
    
    static YYCacheHelper *helper = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        helper = [[YYCacheHelper alloc]init];
    });
    
    return helper;
    
}

/**
 *  异步读取缓存
 *
 *  @param url         缓存key url
 *  @param resultBlock 回调的Block
 */
+(void)isCacheExistWithUrl:(NSString *)url resultBlock:(void(^)(id cacheObj))resultBlock
{
    YYCacheManger *manger = [YYCacheManger manger];
    [manger objectForKey:url withBlock:^(NSString *key, id<NSCoding> object) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (resultBlock) {
                resultBlock(object);
            }
            
        });
        
    }];
}
/**
 *  同步读取缓存
 *
 *  @param url 缓存key URl
 *
 *  @return 缓存内容
 */
+(id)cacheWithURL:(NSString *)url
{
    YYCacheManger *manger = [YYCacheManger manger];
    
    return [manger objectForKey:url];
}

/**
 *  设置缓存
 *
 *  @param url         缓存url
 *  @param responseObj 缓存内容
 */
+ (void)setCacheWithUrl:(NSString *)url responseObj:(id)responseObj
{
    if (url && responseObj) {
        YYCacheManger *manger = [YYCacheManger manger];
        [manger setObject:responseObj forKey:url];
    }
}

@end
